package com.example.coupon.executor.impl;

import com.example.coupon.constant.RuleFlag;
import com.example.coupon.executor.AbstractExecutor;
import com.example.coupon.executor.RuleExecutor;
import com.example.coupon.vo.CouponTemplateSDK;
import com.example.coupon.vo.SettlementInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * 折扣优惠券结算规则执行器
 *
 * @author wangbo
 * @description:
 * @date 2021/2/2011:21
 */
@Slf4j
@Component
public class ZheKouExecutor extends AbstractExecutor implements RuleExecutor {

    @Override
    public RuleFlag ruleConfig() {
        return RuleFlag.ZHEKOU;
    }

    @Override
    public SettlementInfo computeRule(SettlementInfo settlement) {
        double goodsSum = retain2Decimals(goodsCostSum(settlement.getGoodsInfos()));

        SettlementInfo probability = processGoodsTypeNotSatisfy(settlement, goodsSum);
        if (null != probability) {
            log.debug("ZheKou Template Is Not Match To GoodsType");
            return probability;
        }

        //折扣优惠券可以直接使用
        CouponTemplateSDK templateSDK = settlement.getCouponAndTemplateInfos().get(0).getTemplate();
        double quota = (double) templateSDK.getTemplateRule().getDiscount().getQuota();

        //计算使用优惠券之后的价格
        settlement.setCost(retain2Decimals((goodsSum * (quota * 1.0 / 100))) > minCost() ? retain2Decimals((goodsSum * (quota * 1.0 / 100))) : minCost());
        log.debug("Use ZheKou Coupon Make Goods Cost From {} To {}",goodsSum,settlement.getCost());
        return settlement;
    }
}
